package com.yuxi.service.impl;

import com.yuxi.common.NumberUtil;
import com.yuxi.common.SkList;
import com.yuxi.common.StringUtil;
import org.springframework.stereotype.Service;
import com.yuxi.modelExt.RightsExt;
import com.yuxi.dao.IRightsDao;
import com.yuxi.searchExt.RightsSearchExt;
import com.yuxi.pubInter.IBaseDao;
import com.yuxi.pubInter.AbstractBaseService;
import com.yuxi.service.IRightsService;
import com.yuxi.pubInter.exception.SkException;
import com.yuxi.model.Rights;
import com.yuxi.pubInter.BaseSearch;

import javax.annotation.Resource;
import java.util.List;

/**
*    @author sunke
*    @Date 2023-10-13 10:05:04
*    @description RightsService      权限表
*/
@Service(value = "rightsService")
public class RightsServiceImpl extends AbstractBaseService<RightsExt, RightsSearchExt,String> implements IRightsService {

    @Resource(name = "rightsDao")
    private IRightsDao rightsDao;

    @Override
    public IBaseDao<RightsExt, RightsSearchExt,String> getDao() {
        return rightsDao;
    }


    @Override
    public List<RightsExt> tree(int level) {
        List<RightsExt> topList=getTopList();
        if(StringUtil.isNullOrEmpty(topList)){
            return null;
        }
        if(level==1){
            return topList;
        }

        topList.stream().forEach(item->{
            treeByRights(item,level);

        });



        return topList;
    }

    /**
     * 得到所有
     *
     * @return
     */
    @Override
    public List<RightsExt> getTopList() {
        RightsSearchExt rightsSearch = RightsSearchExt.getInstance();
        rightsSearch.setLevelGreater(1);
        rightsSearch.setLevelLess(1);
        rightsSearch.setOrderBy(Rights.SORT_NUM);
        rightsSearch.setOrderDesc(BaseSearch.ASC_ORDER);
        try {
            return getAllList(rightsSearch);
        } catch (SkException e) {

        }
        return null;
    }

    /**
     * 得到用户所有上级
     *
     * @param rightsId
     * @param self     是否要包括自己
     * @return
     */
    @Override
    public List<RightsExt> getUpListByRightsId(String rightsId, boolean self) {


        if (StringUtil.isNullOrEmpty(rightsId)) {
            return null;
        }
        try {
            RightsExt rights = getDetail(rightsId);
            if (StringUtil.isNullOrEmpty(rights)) {
                return null;
            }
            List<RightsExt> resultList = SkList.getInstance();
            if (self) {
                resultList.add(rights);
            }
            getUpRights(resultList, rights.getUpId());

            return resultList;

        } catch (SkException e) {

        }


        return null;
    }

    private void getUpRights(List<RightsExt> resultList, String rightsId) {

        if (StringUtil.isNullOrEmpty(rightsId)) {
            return;
        }
        if (StringUtil.isNullOrEmpty(resultList)) {
            resultList = SkList.getInstance();
        }
        try {
            RightsExt rights = getDetail(rightsId);
            if (StringUtil.isNullOrEmpty(rightsId)) {
                return;
            }
            resultList.add(rights);

            getUpRights(resultList, rights.getUpId());


        } catch (SkException e) {

        }


    }


    @Override
    public void updateAllSubLevelByRights(RightsExt rights, int currentRightsLevel, boolean upSelf) {


        try {
            if (upSelf) {
                rights.setLevel(currentRightsLevel);
                update(rights);
            }
            currentRightsLevel = currentRightsLevel + 1;
            List<RightsExt> subList = getSubListByRightsId(rights.getId());
            if (StringUtil.isNullOrEmpty(subList)) {
                return;
            }
            int finalCurrentRightsLevel = currentRightsLevel;
            subList.stream().forEach(rightsExt -> {
                updateAllSubLevelByRights(rightsExt, finalCurrentRightsLevel, true);

            });


        } catch (SkException e) {

        }

    }

    /**
     * 更新所有下级等级
     *
     * @param rightsId
     * @param currentRightsLevel
     * @param upSelf             是否更新自己
     */
    @Override
    public void updateAllSubLevelByRightsId(String rightsId, int currentRightsLevel, boolean upSelf) {

        try {
            RightsExt rights = getDetail(rightsId);
            updateAllSubLevelByRights(rights, currentRightsLevel, upSelf);


        } catch (SkException e) {

        }


    }


    /**
     * 得到的下级树结构
     *
     * @param rightsId
     * @return
     * @throws SkException
     */
    @Override
    public RightsExt treeByRightsId(String rightsId,int level) throws SkException {
        if (StringUtil.isNullOrEmpty(rightsId)) {
            return null;
        }
        RightsExt rights = getDetail(rightsId);
        List<RightsExt> subList = getSubListByRightsId(rightsId);
        rights.setSubList(subList);
        if (StringUtil.isNotObjEmpty(subList)) {
            subList.stream().forEach(item -> {

                treeByRights(item,level);

            });

        }


        return rights;
    }

    /**
     * 权限的下级
     *
     * @param rights
     * @throws SkException
     */
    @Override
    public void treeByRights(RightsExt rights,int level) {
        try {
            if (StringUtil.isNullOrEmpty(rights)) {
                return;
            }


            List<RightsExt> subList = getSubListByRightsId(rights.getId());

            rights.setSubList(subList);

            if (StringUtil.isNullOrEmpty(subList)) {
                return;
            }

            subList.stream().forEach(item -> {

                if(level!=-1 && NumberUtil.parseInt(item.getLevel())>level){
                    return;
                }
                treeByRights(item,level);

            });


        } catch (SkException e) {

        }
    }

    /**
     * 得到某一个权限的下一级权限
     *
     * @param rightsId
     * @return
     * @throws SkException
     */
    @Override
    public List<RightsExt> getSubListByRightsId(String rightsId) throws SkException {
        RightsSearchExt rightsSearch = RightsSearchExt.getInstance();
        rightsSearch.setUpIdEq(rightsId);
        rightsSearch.setOrderBy(Rights.SORT_NUM);
        rightsSearch.setOrderDesc(BaseSearch.ASC_ORDER);

        return getAllList(rightsSearch);
    }

    /**
     * 得到顶级权限
     *
     * @param rightsId
     * @return
     * @throws SkException
     */
    @Override
    public RightsExt getTopRightsByRightsId(String rightsId) throws SkException {

        if (StringUtil.isNullOrEmpty(rightsId)) {
            return null;
        }

        RightsExt rights = getDetail(rightsId);

        if (StringUtil.isNullOrEmpty(rights.getUpId())) {
            return rights;
        }
        rights = getTopRightsByRightsId(rights.getUpId());

        return rights;
    }

}